<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../../../layui/css/layui.css">
    <!--font-awesome字体图标库-->
    <link rel="stylesheet" type="text/css" href="../../../css/font-awesome.min.css"/>
    <title>药品管理</title>
</head>
<body style="background-color:#fff;">

<div class="layui-fluid">
    <!-- 展示药品管理 start -->
    <div class="layui-row">
        <fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;padding: 15px;">
            <legend>条件搜索</legend>
            <div>
                <form class="layui-form layui-form-pane" lay-filter="search-drug" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">药品名</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" autocomplete="off" placeholder="请输入药品关键词"
                                       class="layui-input" lay-verify="required">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="search-drug">
                                <i class="layui-icon layui-icon-search"></i>
                                搜索
                            </button>
                        </div>
                    </div>

                </form>
            </div>
        </fieldset>


        <table class="layui-hide" id="drugList" lay-filter="drugList"></table>
    </div>
    <!-- 展示药品管理 end -->
</div>

<!-- 编辑、删除按钮 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
        <i class="layui-icon layui-icon-delete"></i>
        删除</a>

    <a class="layui-btn layui-btn-xs" style="background-color: #5bc0de;" lay-event="edit">
        <i class="fa fa-pencil"></i>
        编辑
    </a>

    {{#  if(d.sale_status ==0){ }}
    <a class="layui-btn layui-btn-xs" style="background-color:#d9534f;" lay-event="download">
        <i class="fa fa-arrow-circle-down"></i>&nbsp;
        停售
    </a>
    {{#  }else{ }}
    <a class="layui-btn layui-btn-xs" style="background-color: #337ab7;" lay-event="upload">
        <i class="fa fa-arrow-circle-down"></i>&nbsp;
        上架
    </a>
    {{#  } }}

    <a class="layui-btn layui-btn-xs" style="background-color: #5cb85c;" lay-event="submit-audit">
        <i class="fa fa-check-circle"></i>&nbsp;
        提交审核
    </a>
</script>


<script src="../../../layui/layui.js"></script>
<script>
    layui.use(['jquery', 'table','form'], function () {
        var $ = layui.jquery; // 注册 jquery 组件
        var table = layui.table;// 注册 table 组件
        var form = layui.form;// 注册 form 组件

        //渲染药品表格数据
        table.render({
            elem: '#drugList' //指定原始表格元素选择器（推荐id选择器）
            , url: '/springboot-hospital/drug/show'
            , cellMinWidth: 80
            , cols: [[
                {field: 'drugid', title: '编号', sort: true, align: 'center'},
                {field: 'name', title: '药品名', width: 200, align: 'center'},
                {field: 'spec', title: '规格', width: 90, align: 'center'},
                {field: 'unit', title: '单位', align: 'center'},
                {field: 'product_company', title: '生产企业', width: 230, align: 'center'},
                {field: 'price', title: '销售价格', align: 'center'},
                {
                    field: 'sale_status', title: '销售状态', width: 140, templet: function (d) {
                        if (d.sale_status == 0) {
                            return '<span class="layui-badge layui-bg-green">正常</span>';
                        } else if (d.sale_status == 1) {
                            return '<span class="layui-badge" style="background-color: #d9534f;">停售</span>';
                        }
                    }, align: 'center'
                },
                {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 320, align: 'center'}
            ]] //设置表头
            , page: true //开启分页
            , limit:3
            ,limits:[3,4,5]
        });

        //搜索药品表单提交
        form.on('submit(search-drug)', function (data) {
            //根据条件重新渲染表格
            table.reload('drugList', {
                url: '/springboot-hospital/drug/show'
                , where: {"name": data.field.name} //设定异步数据接口的额外参数
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听行工具事件
        table.on('tool(drugList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                // 执行删除操作
                layer.confirm('真的要删除么', function (index) {
                    // 发送 ajax 请求删除数据
                    $.ajax({
                        url:'/springboot-hospital/drug/remove',
                        data:'drugid='+data.drugid,
                        success:function (result) {
                            if(result.flag){
                                layer.msg('删除成功');
                                table.reload('drugList');
                            }else {
                                layer.msg('删除失败');
                            }
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                //执行更新操作,打开一个弹出层
                layer.open({
                    type: 2,
                    title: '更新药品信息',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area: ['75%', '100%'],
                    content: '/springboot-hospital/iframe/manager/drug/UpdateDrug.html?drugid=' + data.drugid
                });

            } else if (obj.event === 'upload') {
                //药品上架

            } else if (obj.event === 'download') {
                //药品下架

            } else if (obj.event === 'submit-audit') {
                //提交审核

            }
        });

    });
</script>
</body>
</html>
